package com.lee.common.exception;

import com.lee.common.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import java.util.stream.Collectors;

import static com.lee.common.exception.FailureStatus.*;

/**
 * 全局异常处理
 *
 * @author Lee HN
 * @date 2021/4/13 17:55
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(CustomException.class)
    public ResultVO<Void> customException(CustomException exception) {
        return ResultVO.failure(exception.getStatus(), exception.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public ResultVO<Void> bindException(BindException exception) {
        final String message = exception.getBindingResult().getAllErrors().stream()
                .map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
        log.error(message, exception);
        return ResultVO.failure(BAD_PARAM, message);
    }

    @ExceptionHandler(InvalidPropertyException.class)
    public ResultVO<Void> invalidPropertyException(InvalidPropertyException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResultVO<Void> illegalArgumentException(IllegalArgumentException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ExceptionHandler(IllegalStateException.class)
    public ResultVO<Void> illegalStateException(IllegalStateException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_STATUS, exception.getMessage());
    }

    @ExceptionHandler(UnsupportedOperationException.class)
    public ResultVO<Void> unsupportedOperationException(UnsupportedOperationException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_OPERATION, exception.getMessage());
    }

    /*@ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseVO<Void> methodArgumentNotValidException(MethodArgumentNotValidException exception) {
        final String message = exception.getBindingResult().getAllErrors().stream()
                .map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
        return ResponseVO.failure(BAD_PARAM, message);
    }*/

    @ExceptionHandler(TypeMismatchException.class)
    public ResultVO<Void> typeMismatchException(TypeMismatchException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ExceptionHandler(MissingServletRequestPartException.class)
    public ResultVO<Void> missingServletRequestPartException(MissingServletRequestPartException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ExceptionHandler(ServletRequestBindingException.class)
    public ResultVO<Void> servletRequestBindingException(ServletRequestBindingException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }


    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultVO<Void> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ExceptionHandler(HttpMessageConversionException.class)
    public ResultVO<Void> httpMessageConversionException(HttpMessageConversionException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResultVO<Void> httpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(BAD_PARAM, exception.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResultVO<Void> exception(Exception exception) {
        log.error(exception.getMessage(), exception);
        return ResultVO.failure(SERVER_ERROR);
    }

    /*@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(IOException.class)
    public ResponseVO<Void> ioException(IOException exception) {
        exception.printStackTrace();
        return ResponseVO.failure(FailureStatus.SERVER_ERROR);
    }*/

    /*@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(SQLException.class)
    public ResponseVO<Void> sqlException(SQLException exception) {
        exception.printStackTrace();
        return ResponseVO.failure(FailureStatus.SERVER_ERROR);
    }*/

}

